package com.mercury.coffee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mercury.coffee.entity.CoffeeInfoVo;
import com.mercury.coffee.entity.PushCoffee;
import com.mercury.coffee.entity.PushCoffeeLabel;
import com.mercury.coffee.mapper.PushCoffeeMapper;
import com.mercury.coffee.service.PushCoffeeLabelService;
import com.mercury.coffee.service.PushCoffeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mercury.user.entity.CenterUser;
import com.mercury.user.entity.CenterUserTasted;
import com.mercury.user.entity.CenterUserWant;
import com.mercury.user.service.CenterUserService;
import com.mercury.user.service.CenterUserTastedService;
import com.mercury.user.service.CenterUserWantService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 童雅婷
 * @since 2021-11-07
 */
@Service
public class PushCoffeeServiceImpl extends ServiceImpl<PushCoffeeMapper, PushCoffee> implements PushCoffeeService {

    @Autowired
    private PushCoffeeLabelService coffeeLabelService;

    @Autowired
    private CenterUserTastedService tastedService;

    @Autowired
    private CenterUserWantService wantService;

    @Autowired
    private CenterUserService userService;


    /**
     * @date 2021/11/7 20:13
     * @params
     * @return
     * @description 查询所有的咖啡以及他的标签
    */
    @Override
    public List<CoffeeInfoVo> selectAll() {
        List<CoffeeInfoVo> coffeeInfoVoList = baseMapper.selectCoffeeInfoList();
        return coffeeInfoVoList;
    }

    /**
     * @date 2021/11/9 16:37
     * @params coffeeInfoVo
     * @return  id
     * @description 将咖啡插入咖啡表，将所有咖啡标签插入咖啡标签表
    */
    @Override
    public int addCoffee(CoffeeInfoVo coffeeInfoVo) {
        PushCoffee pushCoffee = new PushCoffee();
        BeanUtils.copyProperties(coffeeInfoVo, pushCoffee);
        pushCoffee.setDeleted(false);
        baseMapper.insert(pushCoffee);

        //获取插入的咖啡的id
        HashMap<String, Object> map = new HashMap<>();
        map.put("coffee_name", coffeeInfoVo.getCoffeeName());
        List<PushCoffee> pushCoffees = baseMapper.selectByMap(map);
        int id = pushCoffees.get(0).getCoffeeId();

        //TODO
        String coffeeTypes = coffeeInfoVo.getCoffeeTypes();
        String[] types = coffeeTypes.split(",");
        for (String coffeeType : types) {
            PushCoffeeLabel coffeeLabel = new PushCoffeeLabel();
            coffeeLabel.setCoffeeId(id);
            coffeeLabel.setCoffeeLabel(Integer.parseInt(coffeeType));
            coffeeLabelService.save(coffeeLabel);
        }
        return id;
    }

    /**
     * @date 2021/11/22 8:15
     * @params id
     * @return
     * @description 根据输入的 coffee id来获取咖啡的详细信息
    */
    @Override
    public CoffeeInfoVo getCoffeeInfoById(int id) {
        CoffeeInfoVo coffeeInfo = baseMapper.getCoffeeInfo(id);
        return coffeeInfo;
    }

    /**
     * @date 2021/11/22 8:34
     * @params userId
     * @return
     * @description 调用 tastedService 来获得用户喝过的所有咖啡，再根据咖啡id获得所有的信息
    */
    @Override
    public List<CoffeeInfoVo> getTasteCoffee(String userId) {
        List<CenterUserTasted> userTasted = tastedService.getUserTasted(userId);
        List<CoffeeInfoVo> coffeeInfoList = new ArrayList<>();
        for (CenterUserTasted centerUserTasted : userTasted) {
            CoffeeInfoVo coffeeInfo = getCoffeeInfoById(centerUserTasted.getCoffeeId());
            coffeeInfoList.add(coffeeInfo);
        }
        return coffeeInfoList;
    }

    /**
     * @date 2021/11/22 9:02
     * @params userId
     * @return
     * @description 封装用户想喝的咖啡的信息列表
    */
    @Override
    public List<CoffeeInfoVo> getWantedCoffee(String userId) {
        List<CenterUserWant> userWant = wantService.getUserWanted(userId);
        List<CoffeeInfoVo> coffeeInfoList = new ArrayList<>();
        for (CenterUserWant centerUserWant : userWant) {
            CoffeeInfoVo coffeeInfo = getCoffeeInfoById(centerUserWant.getCoffeeId());
            coffeeInfoList.add(coffeeInfo);
        }
        return coffeeInfoList;
    }

    @Override
    public CoffeeInfoVo getFavouriteCoffee(String userId) {
        String favouriteCoffeeId = userService.getById(userId).getUserFavouriteCoffeeId();
        CoffeeInfoVo coffeeInfo = getCoffeeInfoById(Integer.parseInt(favouriteCoffeeId));
        return coffeeInfo;
    }

    @Override
    public Boolean setFavouriteCoffee(int id, String userId) {
        CenterUser user = userService.getById(userId);
        user.setUserFavouriteCoffeeId(String.valueOf(id));
        return userService.updateById(user);

    }

    @Override
    public Boolean cancelFavouriteCoffee(int id, String userId) {
        CenterUser user = userService.getById(userId);
        user.setUserFavouriteCoffeeId(null);
        return userService.updateById(user);
    }


}
